<?php
namespace app\Model;

class Album extends Model {
	private $id;
	private $title;
	private $description;
	private $art;
	//1表示公开，0表示私密
	private $status;

	/**
	*一个相册包含多张图片
	*
	*@var Image
	*/
	private $imageCollection= [];
	private $create_at;
	private $update_at;
	private $deleted_at;

	/**
	*一个相册属于一个用户
	*
	*@var User
	*/
	private $user;

    protected $dataDirectory = 'album';

	/**
	*还原相簿信息
	*
	*@return array $album 当前未进行新操作的相簿信息
	*/
	public static function fromArray(Array $array)
    {
        if (empty($array) || count($array) == 0) {
            return null;
        }

        $album = new Album();
        $album->id = $array['id'];
        $album->title = $array['title'];
        $album->description = $array['description'];
        $album->art = isset($array['art']) ? $array['art'] : '';
        $album->created_at = time();
        $album->updated_at = $album->created_at;
        $album->user = User::fromArray($array['user']);
        foreach ($array['imageCollection'] as $imageArray) {
            $image = Image::fromArray($imageArray);
            $album->imageCollection[] = $image;
        }
        return $album;
    }

    public function toArray()
    {
        $imageArrayCollection = [];
        foreach ($this->imageCollection as $image) {
            $imageArrayCollection[] = $image->toArray();
        }

        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'art' => $this->art,
            'status' => $this->status,
            'user' => $this->user->toArray(),
            'imageCollection' => $imageArrayCollection
        ];
    }

     public function addImage(Image $image)
    {
        $this->imageCollection[] = $image;
    }

	/**
     *上传多张图片到当前相册
     */
	public function upload()
	{
		$files = $_FILES['imageUpload'];
		$number = count($files['name']);

		$imageTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'];
		$rootDir = PUBLIC_DIR . DIRECTORY_SEPARATOR;
		for ($i=0; $i<$number; $i++) {
			if ($files['error'][$i] == 0) {
				if (in_array($files['type'][$i], $imageTypes)) {
					if ($files['size'][$i] < 1024 * 1024 && $files['size'][$i] > 0) {

						$fileDir = 'uploadImage' . DIRECTORY_SEPARATOR;
						$fileName = hash('sha256', time() . mt_rand() . $i) . '.' . pathinfo($files['name'][$i])['extension'];
						$filePath = $fileDir . $fileName;
						$imageId = md5($filePath);

						$distination = $rootDir . $filePath;
						move_uploaded_file($files['tmp_name'][$i], $distination);
						$image = new Image($imageId, $fileName, $filePath);
						
						$this->addImage($image);
					}
				}
			}
			$this->cover();
		}
	}

	public function cover()
	{
		$defaultCover = 'img' . DIRECTORY_SEPARATOR . 'timg.jpg';
			$artImg = isset($this->imageCollection[0]) ? $this->imageCollection[0] : null;
        	if (!empty($artImg)) {
            	$this->art = $artImg->path;
			}else $this->art = $defaultCover;
	}

	public function __get($name)
	{
		if (property_exists(self::class, $name)) {
			return $this->$name;
		}
		return null;
	}

	public function __set($name, $value)
	{
		if (property_exists(self::class, $name)) {
			$this->$name = $value;
		}
	}
}